/**
 * Created by sky on 8/30/2017.
 */
require('../models/food.model');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
let foodModel = mongoose.model('foods');

require('../models/merchant.model');
let merchantModel = mongoose.model('merchants');

mongoose.connect(
    'mongodb://localhost:27017/ACM',
    {useMongoClient: true}
).then(function (db) {
    console.log('DB connected!');
}).catch(function (err) {
    console.log('DB connect failed!');
    console.log(err.stack);
});

let merchant = new merchantModel({
    "name" : "m_jordan",
    "password": "123",
    "idCard": "ddd",
    "avator": "ddd", 
    "mStatus": "pass", 
    "address": "addr",
    },{ versionKey:false}
);


// merchant.save(function (err, doc) {
//     if (err) return console.error(err);
//     else console.log(doc.toJSON());
// });

//  foodModel.find({merchant:{$in:["59a914bfafb7ef3324f94b66"]}}, function(err, foods){
//         if(err) {
//             console.error(err);
//         }
//         console.log(err, foods);
//     });

//根据种类查找
// foodModel.find({categories:"西餐","merchant":"59a914bfafb7ef3324f94b66"},function(err, foods){
//         if(err) {
//             console.error(err);
//         }
//         console.log(err, foods);
//     });

// foodModel.find({merchant:{$in:["59a914bfafb7ef3324f94b66"]}}, function(err, foods){
//         if(err) {
//             console.error(err);
//         }
//         console.log(err, foods);
//     }).populate('merchant').exec();

//增加食品
let food = new foodModel({
    "name": "test",
    "price": 11,
    "image": "test",
    "categories": "test",
    "merchant":"59a914bfafb7ef3324f94b66"
});

function addFood(food,callback) {
    console.log("add food facade");
    let foodDB = new foodModel(food);
    foodDB.save(function(err,result){
         if(err) {
            console.error(err);
        }
        console.log(err, result);
        callback(null, result);
    })
};

addFood(food,function(){})